﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YJKRevitKernel.RevitDimensionUtility;
using YJKRevitDimensionText.Commands;
using YJKRevitDimensionText.Utility;

namespace YJKRevitDimension.MapNameDimension
{
	public partial class DlgMapNameDim : System.Windows.Forms.Form
	{
		public bool InfoChanged
		{
			get
			{
				return this.m_InfoChanged;
			}
			set
			{
				this.m_InfoChanged = value;
			}
		}

		public DlgMapNameDim(Autodesk.Revit.DB.Document doc)
		{
			this.m_Doc = doc;
			this.InitializeComponent();
		}

		public DlgMapNameDim(string mapName, Autodesk.Revit.DB.Document doc)
		{
			this.m_Doc = doc;
			this.InitializeComponent();
			this.defaultName = mapName;
		}

		private void DlgMapNameDim_Load(object sender, EventArgs e)
		{
			this.InitViewScale();
			this.m_Initialize = false;
			Helper.AddCombItems(DimDate.FontFamily, this.cboMapNameFontType);
			Helper.AddCombItems(DimDate.FontFamily, this.cboFontType);
			this.rdbTradition.Checked = true;
			if (this.cboMapNameFontType.Items.Count > 0)
			{
				string text = this.TextName(DimDate.FontFamily, true);
				if (text != null && text != string.Empty)
				{
					this.cboMapNameFontType.Text = text;
				}
				else
				{
					this.cboMapNameFontType.SelectedIndex = 0;
				}
			}
			if (this.cboFontType.Items.Count > 0)
			{
				string text2 = this.TextName(DimDate.FontFamily, false);
				if (text2 != null && text2 != string.Empty)
				{
					this.cboFontType.Text = text2;
				}
				else
				{
					this.cboFontType.SelectedIndex = 0;
				}
			}
			this.FontType = this.cboMapNameFontType.SelectedItem.ToString();
			this.MapNameStr = this.defaultName;
			this.ckbDisplayProportion.Checked = true;
			this.ckbDisplayProportion.Checked = true;
			this.ReadDlgData();
			this.traditionOrNational = this.rdbTradition.Checked;
			this.isChecked = this.ckbDisplayProportion.Checked;
			this.FontType = this.cboMapNameFontType.SelectedItem.ToString().Trim();
			if (this.cboFontType.SelectedItem != null)
			{
				this.proportionFontType = this.cboFontType.SelectedItem.ToString();
			}
			this.txtMapNameStr.Text = this.defaultName;
			this.MapNameStr = this.txtMapNameStr.Text;
			this.m_Initialize = true;
		}

		private string TextName(List<string> lstTextTypeNames, bool bIsMapName = true)
		{
			string result = string.Empty;
			if (this.cboFontType.Items.Count > 0)
			{
				List<string> list = new List<string>();
				List<string> list2 = new List<string>();
				if (Convert.ToInt32(this.m_Doc.Application.VersionNumber) > 2016)
				{
					if (bIsMapName)
					{
						list.Add("HY_黑体_5.5mm");
						list.Add("黑体_5.5mm");
						list2.Add("HY_黑体");
						list2.Add("黑体");
						list2.Add("_5.5mm");
					}
					else
					{
						list.Add("HY_黑体_4.0mm");
						list.Add("黑体_4.0mm");
						list.Add("HY_黑体_4mm");
						list.Add("黑体_4mm");
						list2.Add("HY_黑体");
						list2.Add("黑体");
						list2.Add("_4.0mm");
						list2.Add("_4mm");
					}
				}
				else if (bIsMapName)
				{
					list.Add("HY_黑体_7.0mm");
					list.Add("黑体_7.0mm");
					list.Add("HY_黑体_7mm");
					list.Add("黑体_7mm");
					list2.Add("HY_黑体");
					list2.Add("黑体");
					list2.Add("_7.0mm");
					list2.Add("_7mm");
				}
				else
				{
					list.Add("HY_黑体_5.0mm");
					list.Add("黑体_5.0mm");
					list.Add("HY_黑体_5mm");
					list.Add("黑体_5mm");
					list2.Add("HY_黑体");
					list2.Add("黑体");
					list2.Add("_5.0mm");
					list2.Add("_5mm");
				}
                result = YJKRevitKernel.RevitDimensionUtility.RevitDimensionUtiltyMethod.GetFortText(lstTextTypeNames, list, list2);
			}
			return result;
		}

		private void InitViewScale()
		{
			List<int> list = new List<int>();
			foreach (Element element in new FilteredElementCollector(this.m_Doc).OfClass(typeof(Autodesk.Revit.DB.View)).ToElements())
			{
				if (element != null)
				{
					Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					if (view != null)
					{
						int scale = view.Scale;
						if (list.FindIndex((int value) => value == scale) == -1)
						{
							list.Add(scale);
						}
					}
				}
			}
			list.Sort();
			foreach (int num in list)
			{
				this.cboProportion.Items.Add("1:" + num.ToString("f0"));
			}
			int num2 = list.FindIndex((int value) => value == this.m_Doc.ActiveView.Scale);
			if (num2 == -1)
			{
				this.cboProportion.SelectedIndex = 0;
				return;
			}
			this.cboProportion.SelectedIndex = num2;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void DlgMapNameDim_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK && (this.MapNameStr == null || this.MapNameStr.Length == 0))
			{
				MessageBox.Show("图名不能为空", "盈建科提示");
				e.Cancel = true;
				return;
			}
			this.SaveDlgData();
			this.SendESC();
		}

		private void SaveDlgData()
		{
			string text = Path.GetTempPath() + this.m_Doc.Application.VersionNumber;
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			FileStream fileStream = new FileStream(text + "\\dmnd.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = this.rdbTradition.Checked ? "0" : "1";
			streamWriter.WriteLine(value);
			value = (this.ckbDisplayProportion.Checked ? "0" : "1");
			streamWriter.WriteLine(value);
			streamWriter.WriteLine(this.txtMapNameStr.Text);
			streamWriter.WriteLine(this.cboProportion.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboMapNameFontType.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboFontType.SelectedIndex.ToString());
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadDlgData()
		{
			string path = Path.GetTempPath() + this.m_Doc.Application.VersionNumber + "\\dmnd.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return true;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			if (Convert.ToInt32(streamReader.ReadLine()) == 0)
			{
				this.rdbTradition.Checked = true;
			}
			else
			{
				this.rdbNational.Checked = true;
			}
			if (Convert.ToInt32(streamReader.ReadLine()) == 0)
			{
				this.ckbDisplayProportion.Checked = true;
				this.cboProportion.Enabled = true;
				this.cboFontType.Enabled = true;
			}
			else
			{
				this.ckbDisplayProportion.Checked = false;
				this.cboProportion.Enabled = false;
				this.cboFontType.Enabled = false;
			}
			this.txtMapNameStr.Text = streamReader.ReadLine();
			int num = Convert.ToInt32(streamReader.ReadLine());
			num = Convert.ToInt32(streamReader.ReadLine());
			if (this.cboMapNameFontType.Items.Count > num)
			{
				this.cboMapNameFontType.SelectedIndex = num;
			}
			num = Convert.ToInt32(streamReader.ReadLine());
			if (this.cboFontType.Items.Count > num)
			{
				this.cboFontType.SelectedIndex = num;
			}
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return true;
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgMapNameDim_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetFocusMessage();
				if (this.m_Initialize && this.InfoChanged)
				{
					this.SendESC();
					return;
				}
			}
			else
			{
				base.Focus();
			}
		}

		private void SendESC()
		{
		}

		private void SendSetFocusMessage()
		{
		}

		private void rdbTradition_CheckedChanged(object sender, EventArgs e)
		{
			if (this.traditionOrNational == this.rdbTradition.Checked)
			{
				this.m_InfoChanged = false;
			}
			else if (this.m_Initialize)
			{
				this.m_InfoChanged = true;
				this.SendSetFocusMessage();
				this.SendESC();
			}
			if (this.rdbTradition.Checked)
			{
				this.traditionOrNational = true;
				return;
			}
			this.traditionOrNational = false;
		}

		private void ckbDisplayProportion_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isChecked == this.ckbDisplayProportion.Checked)
			{
				this.m_InfoChanged = false;
			}
			else if (this.m_Initialize)
			{
				this.m_InfoChanged = true;
				this.SendSetFocusMessage();
				this.SendESC();
			}
			if (this.ckbDisplayProportion.Checked)
			{
				this.isChecked = true;
				this.cboFontType.Enabled = true;
				this.cboProportion.Enabled = true;
				return;
			}
			this.isChecked = false;
			this.cboFontType.Enabled = false;
			this.cboProportion.Enabled = false;
		}

		private void cboMapNameFontType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboMapNameFontType.SelectedItem == null)
			{
				return;
			}
			string text = this.cboMapNameFontType.SelectedItem.ToString().Trim();
			if (this.FontType.CompareTo(text) == 0)
			{
				this.m_InfoChanged = false;
			}
			else if (this.m_Initialize)
			{
				this.m_InfoChanged = true;
				this.SendSetFocusMessage();
				this.SendESC();
			}
			this.FontType = text;
			this.SendSetFocusMessage();
		}

		private void cboProportion_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboProportion.SelectedItem == null)
			{
				return;
			}
			string strB = this.cboProportion.SelectedItem.ToString();
			if (this.proportionSelect.CompareTo(strB) == 0)
			{
				this.m_InfoChanged = false;
			}
			else if (this.m_Initialize)
			{
				this.m_InfoChanged = true;
				this.SendSetFocusMessage();
				this.SendESC();
			}
			this.proportionSelect = strB;
			this.SendSetFocusMessage();
		}

		private void cboFontType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboFontType.SelectedItem == null)
			{
				return;
			}
			string strB = this.cboFontType.SelectedItem.ToString();
			if (this.proportionFontType.CompareTo(strB) == 0)
			{
				this.m_InfoChanged = false;
			}
			else if (this.m_Initialize)
			{
				this.m_InfoChanged = true;
				this.SendSetFocusMessage();
				this.SendESC();
			}
			this.proportionFontType = strB;
			this.SendSetFocusMessage();
		}

		private void txtMapNameStr_TextChanged(object sender, EventArgs e)
		{
			if (this.MapNameStr.CompareTo(this.txtMapNameStr.Text) == 0)
			{
				this.m_InfoChanged = false;
			}
			else if (this.m_Initialize)
			{
				this.m_InfoChanged = true;
			}
			this.MapNameStr = this.txtMapNameStr.Text;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.AddChar2TextBox("°");
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.AddChar2TextBox("±");
		}

		private void button3_Click(object sender, EventArgs e)
		{
			this.AddChar2TextBox("φ");
		}

		private void button4_Click(object sender, EventArgs e)
		{
			this.AddChar2TextBox("%");
		}

		private void AddChar2TextBox(string strVal)
		{
			int selectionStart = this.txtMapNameStr.SelectionStart;
			this.txtMapNameStr.Text.Substring(0, selectionStart);
			string text = this.txtMapNameStr.Text;
			text = text.Insert(selectionStart, strVal);
			this.txtMapNameStr.Text = text;
			this.txtMapNameStr.SelectionStart = selectionStart + strVal.Length;
			this.txtMapNameStr.Focus();
		}

		private Document m_Doc;

		public string FontType = "";

		public string MapNameStr = "";

		public bool traditionOrNational = true;

		public string proportionSelect = "";

		public bool isChecked = true;

		public string proportionFontType = "";

		private string defaultName = "";

		private bool m_Initialize;

		private bool m_InfoChanged;
	}
}
